using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum TaskTypeEnum
{
    none,
    /// <summary>
    /// 1.登录奖励
    /// </summary>
    login,
    /// <summary>
    /// 2.观看广告
    /// </summary>
    adv,//
    /// <summary>
    /// 3.累计通关
    /// </summary>
    pass,//
    /// <summary>
    /// 4.累计使用道具
    /// </summary>
    useprop,//
}

[Serializable]
public class TaskConfig2
{
    public TaskConfig[] tasklist;
}
//任务信息
[Serializable]
public class TaskConfig
{
    public int taskId;//""：任务ID
    public string desc;//"desc": 描述
    public int taskType;//"taskType":道具类型1.登录奖励2.观看广告3.累计通关4.累计使用道具
    public int propID;//"propID":需要完成的任务 道具id
    public int targetNum;//"targetNum":需要完成的任务 道具累计数量
    public int rewardID;//"rewardID":奖励的道具ID
    public int rewardNum;//"rewardNum":奖励的道具数量
}
public class TaskManager
{
    private static TaskConfig[] mTaskInfoArr;
    //任务完成度情况
    private static Dictionary<int, int[]> mTaskCompleteDic = new Dictionary<int, int[]>();
    
    //任务的taskid
    private static Dictionary<TaskTypeEnum, List<int>> mTaskCompleteTaskidDic = new Dictionary<TaskTypeEnum, List<int>>();

    public static TaskConfig[] OnGetTaskInfo()
    {
        return mTaskInfoArr;
    }
    /// <summary>
    /// 加载本地任务表
    /// </summary>
    public static void OnLoadLocalTask()
    {
        var shuju = ResourceManager.Instance.LoadJsonAsset("Hall/Table", "taskInfo");
        mTaskInfoArr = JsonUtility.FromJson<TaskConfig2>(shuju.text).tasklist;

        for (int i = 0; i < mTaskInfoArr.Length; i++)
        {
            mTaskCompleteDic.Add(mTaskInfoArr[i].taskId,new int[2]{0,mTaskInfoArr[i].targetNum});
            TaskTypeEnum type = (TaskTypeEnum)mTaskInfoArr[i].taskType;
            if (!mTaskCompleteTaskidDic.ContainsKey(type))
            {
                mTaskCompleteTaskidDic.Add(type,new List<int>());
            }
            mTaskCompleteTaskidDic[type].Add(mTaskInfoArr[i].taskId);
        }

        //如果是当天就加载本地的数据
        if (OnGetLocalIsSameDay())
        {
            OnGetLocalTaskInfo();
        }
    }

    public static Dictionary<int, int[]> OnGetTaskCompleteDic()
    {
        return mTaskCompleteDic;
    }
    /// <summary>
    /// 设置自己的任务完成状态
    /// </summary>
    public static void OnSetTaskCompleteStatusById(int key,int addnum)
    {
        if (mTaskCompleteDic.ContainsKey(key))
        {
            int curnum = mTaskCompleteDic[key][0];
            int maxnum = mTaskCompleteDic[key][1];
            if (addnum == -1)
            {
                curnum = -1;//表示领取过了
            }
            else if ((curnum + addnum) > maxnum)
            {
                curnum = maxnum;
            }
            else
            {
                curnum += addnum;
            }
            mTaskCompleteDic[key][0] = curnum;
        }
    }

    /// <summary>
    /// 给特定类型的任务添加数量
    /// </summary>
    /// <param name="tasktype"></param>
    public static void OnAddTaskNumByTaskType(TaskTypeEnum tasktype)
    {
        if (mTaskCompleteTaskidDic.ContainsKey(tasktype))
        {
            var taskidlist = mTaskCompleteTaskidDic[tasktype];
            for (int i = 0; i < taskidlist.Count; i++)
            {
                if (mTaskCompleteDic[taskidlist[i]][0] >= 0)
                {
                    OnSetTaskCompleteStatusById(taskidlist[i], 1);
                }
            }
            OnGetLocalTaskInfo(true);
        }
    }
    
    public static void OnGetLocalTaskInfo(bool isset = false)
    {
        string taskkey = "LOCAL_LOCAL_TASK_INFO_1";
        if (isset)
        {
            string savelocalvalue = "";
            foreach (var item in mTaskCompleteDic)
            {
                savelocalvalue += string.Format("{0},{1}_", item.Key, item.Value[0]);
            }
            Debug.Log("task cun:"+savelocalvalue);
            PlayerPrefs.SetString(taskkey,savelocalvalue);
        }
        else
        {
            if (PlayerPrefs.HasKey(taskkey))
            {
                Debug.Log("task:"+PlayerPrefs.GetString(taskkey));
                string[] tasklocalinfo = PlayerPrefs.GetString(taskkey).Split("_");
                for (int i = 0; i < tasklocalinfo.Length-1; i++)
                {
                    string[] localtaskarr = tasklocalinfo[i].Split(",");
                    int key = int.Parse(localtaskarr[0]);
                    int value = int.Parse(localtaskarr[1]);
                    OnSetTaskCompleteStatusById(key, value);
                }
            }
        }
    }
    
    /// <summary>
    /// 获取是不是当天
    /// </summary>
    /// <returns></returns>
    public static bool OnGetLocalIsSameDay(bool isset = false)
    {
        string taskkey = "LOCAL_LOCAL_TASK_TIMER_KEY_1";
        if (isset)
        {
            int year = DateTime.Now.Year;
            int mon = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            PlayerPrefs.SetString(taskkey,string.Format("{0}_{1}_{2}",year,mon,day));
        }
        else
        {
            if (PlayerPrefs.HasKey(taskkey))
            {
                int year = DateTime.Now.Year;
                int mon = DateTime.Now.Month;
                int day = DateTime.Now.Day;
                string[] shijian = PlayerPrefs.GetString(taskkey).Split("_");
                int localyear = int.Parse(shijian[0]);
                int localmon = int.Parse(shijian[1]);
                int localday = int.Parse(shijian[2]);
                Debug.LogError("年："+year+"="+localyear+",月："+mon+"="+localmon+"日："+day+"="+localday);
                if (year == localyear && mon == localmon && day == localday)
                {
                    return true;
                }
            }
        }
        return false;
    }
}
